package Set;

import java.util.HashSet;
import java.util.Set;

public class HashSetQuZhong {
    public static void main(String[] args) {
        Student student1 = new Student("ton", 19, "man", 99.0);//0*000011
        Student student2 = new Student("jon", 29, "man", 99.0);
        Student student3 = new Student("son", 39, "man", 99.0);
        Student student4 = new Student("lon", 49, "man", 99.0);
        Student student5 = new Student("lon", 49, "man", 99.0);//0*000022
        Set<Student> studentSet = new HashSet<Student>();//ctrl+shift+o导包
        studentSet.add(student1);//0*000011
        studentSet.add(student2);
        studentSet.add(student3);
        studentSet.add(student4);
        studentSet.add(student1);//0*000011,去掉重复,调用equals-->Object提供的equals方法,内部使用this==obj惊醒方法
       studentSet.add(student5);//地址不一样.内容重复
        for (Student stu : studentSet) {
            System.out.println(stu.toString());
        }
        //HashSet没有必要插入新数据没有必要与已有的数据进行一一比较
        //HashSet原有equals方法进行比较,是具有前提的(俩个对象的哈希值是相同的)
        System.out.println(student4.equals(student5));//true
        System.out.println(student4.hashCode());
        System.out.println(student5.hashCode());
    }
}

class Student {
    String name;
    Integer age;
    String sex;
    Double score;

    public Student() {
    }

    public Student(String name, Integer age, String sex, Double score) {
        this.name = name;
        this.age = age;
        this.sex = sex;
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", sex=" + sex +
                ", score=" + score +
                '}';
    }
    @Override
    public  boolean equals(Object object){
        if (this == object) {
            return  true;
        }
        if (object == null) {//对象为空
            return false;
        }
        if (this.getClass() != object.getClass()) {//类型不一样
            return false;
        }
        Student s =(Student)object;
       if(this.name.equals(s.name)&&this.age.equals(s.age)&&this.sex.equals(s.sex)&&this.score.equals(s.score)){
           return true;
       }
       return  false;

        }
        //hashCode才是调用equals的前提
    //只有哈希码相同,才能用equals
        public  int hashCode(){
        return  123;
        }
    }
